package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.domain.Sequence;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

public class ConfirmOrderServlet extends HttpServlet {
    private static final String VIEW_ORDER = "/WEB-INF/jsp/order/ViewOrder.jsp";
    private Order order;
    private OrderService orderService;
    private Cart cart;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        HttpSession session = req.getSession();
        order = (Order)session.getAttribute("order");

        if (order!=null){
            orderService = new OrderService();
            orderService.insertOrder(order);

            cart = null;
            session.setAttribute("cart",cart);

            Account account = (Account)session.getAttribute("account");

            AccountService accountService = new AccountService();
            accountService.deleteALL(account);





            if(account != null){
                HttpServletRequest httpRequest= req;
                String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                LogService logService = new LogService();

                String logInfo = logService.logInfo(" ") + strBackUrl + account.getUsername()+" 确定订单";
                logService.insertLogInfo(account.getUsername(), logInfo);
            }

            req.getRequestDispatcher(VIEW_ORDER).forward(req,resp);
        }else {

        }


    }
}
